import { observable, action } from 'mobx';
import { WeaTools } from 'ecCom';
import moment from 'moment';
import * as API from '../apis/shift';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmShift {
  title = () => getLabel('125798',"我的排班");
  newChecking = () => getLabel('385037',"新建考勤流程");
  leftTitle= () => getLabel('32997',"人员组织");
  dateTabDatas = [
    {title:'年',key:"1"},
    {title:'月',key:"2"}
  ];
  hrmId = undefined;

  @observable spinning = false;
  @observable yearData = {};
  @observable calendarData = {};
  @observable holidayData = {};
  @observable hrmName = WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
  @observable loading = false;
  @observable dateTabkey = '2';
  @observable userRoot = undefined;
  @observable pDate = new Date();
  @observable hasRight = true;
  @observable canSearch = true;

  @action init = () => {
    this.dateTabkey = '2';
    this.pDate = new Date();
    this.hrmId = undefined;
    this.hrmName = WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
    this.userRoot = undefined;
    this.getData();
  }

  @action getData = () => {
    let params = {};
    if (this.hrmId) params.resourceId = this.hrmId;
    if(this.dateTabkey == '1'){
      params.kqDate = moment(this.pDate).format('YYYY');
    }else{
      params.kqDate = moment(this.pDate).format('YYYY-MM');
    }
    params.type = this.dateTabkey;
    this.spinning = true;
    API.getHrmScheduleReportInfo(params).then((data)=> {
      if (data.status == '1') {
        this.calendarData = data.result;
        this.holidayData = data.holiday;
        this.canSearch = data.canSearch;
        this.hasRight = data.hasRight == null ? true : data.hasRight;
      } else {
        this.hasRight = false;
      }
      this.spinning = false;
    }, error => this.spinning = false)
  }

  @action changeDateTabHandler = (key) => {
    this.dateTabkey = key;
  }

  @action changeDateHandler = (v) => {
    this.pDate = v;
    this.getData();
  }

  @action updateDateTabkey = (key) => this.dateTabkey = key;

  @action resetUserRoot = () => {
    this.userRoot = null;
    this.hrmId = null;
    this.hrmName = WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
    this.getData();
  }

  @action updateUserRoot = (ids, names, datas) => this.userRoot = datas;

  @action updateHrm = (id, name) => {
    this.hrmId = id;
    this.hrmName = name || WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
    this.getData();
  }
}

export const hrmShift = new HrmShift();